﻿using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System;
using System.Threading;

namespace ZhiHuSpider
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Go(30000000, 35000000);
        }


        public void Go(int iStart, int iEnd)
        {
            List<string> author = new List<string>();
            service service = new service();
            for (int i = iStart; i < iEnd; i++)
            {

                ZhiHuModel zhm = new ZhiHuModel();
                
                try
                {
                    zhm = service.Analytical(service.Spider("http://www.zhihu.com/question/" + i), "http://www.zhihu.com");
                }
                catch (Exception e)
                {
                    continue;
                }
                try
                {
                    SqlDBConnect sqldbc = new SqlDBConnect();
                    List<string> sqls = new List<string>();
                    string sql1 = "insert into T_Question(questionId,title,detail) values ('{0}','{1}','{2}')";
                    sql1 = String.Format(sql1, i, zhm.questionTitle, zhm.questionDetail);
                    sqls.Add(sql1);
                    List<AnswerModel> answerList = zhm.answer;
                    foreach (AnswerModel am in answerList)
                    {
                        string sql = "insert into T_Answer (questionId,author,[content]) values ('{0}','{1}','{2}')";
                        sql = String.Format(sql, i, am.answerAuthor, am.answerContent);
                        sqls.Add(sql);
                    }
                    List<AskModel> askList = zhm.ask;
                    if (askList.Count > 0)
                    {
                        foreach (AskModel askModel in askList)
                        {
                            string sql = "";
                            if (!author.Contains(askModel.title))
                            {
                                sql = "insert into T_Ask (title,author) values ('{0}','{1}')";
                                sql = String.Format(sql, askModel.title, askModel.author);
                                author.Add(askModel.title);
                            }

                            sqls.Add(sql);
                        }
                    }
                    sqldbc.ExecTansaction(sqls);
                }
                catch (Exception e)
                {
                    FileStream fs = new FileStream(@"d://log.txt", FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(i + " -- " + e.Message + "\n");
                    sw.Close();
                    fs.Close();

                    continue;
                }
            }
        }

        public string FilterSpecial(string strHtml)
        {
            if (string.Empty == strHtml)
            {
                return strHtml;
            }
            string[] aryReg = { "'", "'delete", "?", "<", ">", "%", "\"\"", ",", ".", ">=", "=<", "_", ";", "||", "[", "]", "&", "/", "-", "|", " ", "''" };
            for (int i = 0; i < aryReg.Length; i++)
            {
                strHtml = strHtml.Replace(aryReg[i], string.Empty);
            }
            return strHtml;
        }
    }
}
